﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.ProfessionalTeam.BLL;
using Yipusoft.ProfessionalTeam.DAL;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;

public partial class MapInfo_ProfessionalTeam_ProsStandard : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDdlProsType();
            BindddlVehicleClass();
            BindVehicleList();

            BindddlDeviceClass();
            BindDeviceList(0);

            bindSoldiersCount();
        }
    }
    void BindddlVehicleClass()
    {
        BindCodeItemList(ddlVehicleClass, "10");
    }
    private void BindCodeItemList(DropDownList ddl, string parentID)
    {
        ListItem li;
        List<CodeItemInfo> items = CodeItem.GetList2(" GroupID='XFZBLB' and ParentID='" + parentID + "' and ItemID<>'1099' order by SeqID,ItemID ");
        foreach (CodeItemInfo item in items)
        {
            li = new ListItem();
            li.Text = "".PadRight(2 * (item.ItemLevel - 1), '　') + item.ItemName;
            li.Value = item.ItemID;
            ddl.Items.Add(li);
            BindCodeItemList(ddl, item.ItemID);
        }
    }
    void BindDdlProsType()
    {
        TeamDataContext tdc = new TeamDataContext();
        ddlProsType.DataSource = tdc.ProcsType;
        ddlProsType.DataBind();
    }
    void BindVehicleList()
    {
        TeamDataContext tdc = new TeamDataContext();
        List<ProcsStandard> list = tdc.ProcsStandard.Where(o => o.IsVehicles == 1 && o.TypeId.Value.ToString() == ddlProsType.SelectedValue).ToList();
        gv.DataSource = list;
        gv.DataBind();

    }
    protected void bntSave1_Click(object sender, EventArgs e)
    {
        TeamDataContext tdc = new TeamDataContext();
        ProcsStandard standard = new ProcsStandard();
        standard.CodeItemId = ddlVehicleClass.SelectedValue;
        standard.Count = Convert.ToInt32(txtVehicleCount.Text);

        standard.IsVehicles = 1;
        standard.IsWhole = 1;
        standard.Count0 = 0;
        standard.IsWhole0 = 0;
        standard.TypeId = Convert.ToInt32(ddlProsType.SelectedValue);
        tdc.ProcsStandard.InsertOnSubmit(standard);
        tdc.SubmitChanges();
        BindVehicleList();
    }
    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
            ProcsStandard standard = e.Row.DataItem as ProcsStandard;
            Label DeviceClass = e.Row.FindControl("DeviceClass") as Label;
            DeviceClass.Text = CodeItem.GetCodeItem(standard.CodeItemId, "XFZBLB").ItemName;
            Label lblCount = e.Row.FindControl("lblCount") as Label;
            lblCount.Text = standard.Count.Value.ToString();

        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            TeamDataContext tdc = new TeamDataContext();
            ProcsStandard standard = tdc.ProcsStandard.Where(o => o.Id == Convert.ToInt32(e.CommandArgument)).FirstOrDefault();
            if (standard != null)
            {
                tdc.ProcsStandard.DeleteOnSubmit(standard);
                tdc.SubmitChanges();
                BindVehicleList();
            }
        }
    }
    protected void ddlProsType_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindVehicleList();
        BindDeviceList(0);

        bindSoldiersCount();
    }

    //装备标准添加
    protected void bntSave2_Click(object sender, EventArgs e)
    {
        TeamDataContext tdc = new TeamDataContext();
        ProcsStandard standard = new ProcsStandard();
        standard.CodeItemId = ddlDeviceClass.SelectedValue;
        standard.Count = Convert.ToInt32(txtDeviceCount.Text);

        standard.IsVehicles = 0;
        standard.IsWhole = Convert.ToInt32(rblDeviceUnit.SelectedValue);
        standard.IsWhole0 = Convert.ToInt32(rblDeviceUnit0.SelectedValue);
        standard.Count0 = Convert.ToInt32(txtDeviceCount0.Text);
        standard.TypeId = Convert.ToInt32(ddlProsType.SelectedValue);
        tdc.ProcsStandard.InsertOnSubmit(standard);
        tdc.SubmitChanges();
        BindDeviceList(0);
    }
    protected void GVDevice_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
            ProcsStandard standard = e.Row.DataItem as ProcsStandard;
            Label DeviceClass = e.Row.FindControl("DeviceClass") as Label;
            DeviceClass.Text = CodeItem.GetCodeItem(standard.CodeItemId).ItemName;
            Label lblCount = e.Row.FindControl("lblCount") as Label;
            lblCount.Text = standard.Count.Value.ToString();
            if (standard.IsWhole == 0)
            {
                lblCount.Text += "/人";
            }
            else
            {
                lblCount.Text += "/组";
            }

            Label lblCount0 = e.Row.FindControl("lblCount0") as Label;
            lblCount0.Text = standard.Count0.Value.ToString();
            if (standard.IsWhole0 == 0)
            {
                lblCount0.Text += "/人";
            }
            else
            {
                lblCount0.Text += "/组";
            }
        }
    }
    protected void GVDevice_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            TeamDataContext tdc = new TeamDataContext();
            ProcsStandard standard = tdc.ProcsStandard.Where(o => o.Id == Convert.ToInt32(e.CommandArgument)).FirstOrDefault();
            if (standard != null)
            {
                tdc.ProcsStandard.DeleteOnSubmit(standard);
                tdc.SubmitChanges();
                BindDeviceList(0);
            }
        }

    }
    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        BindDeviceList(PageIndex);
    }
    void BindddlDeviceClass()
    {
        BindCodeItemList2(ddlDeviceClass, "ZBQC010000");
    }
    void BindDeviceList(int pageIndex)
    {
        TeamDataContext tdc = new TeamDataContext();
        ListPageContral1.ItemCount = tdc.ProcsStandard.Where(o => o.IsVehicles == 0 && o.TypeId.Value.ToString() == ddlProsType.SelectedValue).Count();
        List<ProcsStandard> list = tdc.ProcsStandard.Where(o => o.IsVehicles == 0 && o.TypeId.Value.ToString() == ddlProsType.SelectedValue).Skip(pageIndex * ListPageContral1.PageSize).Take(ListPageContral1.PageSize).ToList();
        GVDevice.DataSource = list;
        GVDevice.DataBind();
    }

    private void BindCodeItemList2(DropDownList ddl, string parentID)
    {
        ListItem li;
        List<CodeItemInfo> items = CodeItem.GetList("ParentID='" + parentID + "' ORDER BY SeqID");
        foreach (CodeItemInfo item in items)
        {
            li = new ListItem();
            li.Text = "".PadRight(2 * (item.ItemLevel - 1), '　') + item.ItemName;
            li.Value = item.ItemID;
            ddl.Items.Add(li);
            BindCodeItemList2(ddl, item.ItemID);
        }
    }

    protected void btnSave3_Click(object sender, EventArgs e)
    {
        TeamDataContext tdc = new TeamDataContext();
        ProcsType type = tdc.ProcsType.Where(o => o.Id.ToString() == ddlProsType.SelectedValue).FirstOrDefault();
        type.SoldiersCount = Convert.ToInt32(txtSoldiersCount.Text);
        tdc.SubmitChanges();
        bindSoldiersCount();
    }
    void bindSoldiersCount()
    {
        TeamDataContext tdc = new TeamDataContext();
        ProcsType type = tdc.ProcsType.Where(o => o.Id.ToString() == ddlProsType.SelectedValue).FirstOrDefault();
        txtSoldiersCount.Text = type.SoldiersCount.Value.ToString();
    }
}
